/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ptr.ptrsystem.servlet;

import com.ptr.controladores.conexionsqlserver;
import com.ptr.ptrsystem.dao.Lista;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author WALDIR
 */
@WebServlet(name = "BusquedaAniDefuncionServlet", urlPatterns = {"/BusquedaAniDefuncion"})
public class BusquedaAniDefuncionServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();


        String CodPersona = request.getParameter("NAP");
        String nroActaCDEF = request.getParameter("NAD");

        String nombre = request.getParameter("p_nom");
        String ape1 = request.getParameter("p_ape");
        String ape2 = request.getParameter("s_ape");

        String BTN = request.getParameter("btn1");

        if (BTN.equals("ActaPersonal")) {
            try {
                conexionsqlserver con = new conexionsqlserver();
                Connection cn = null;
                Statement st = null;
                ResultSet rs = null;
                String QQ = "select * from TB_DEFUNCION d,TB_PERSONA p,TB_UBIGEO ub,TB_CENT_LUG_OCURRE oc where (d.COD_PERSONA=p.COD_PERSONA and d.IDUBIGEO=ub.idubigeo and oc.COD_LUGAR_OCURR=d.COD_LUGAR_OCURR) and d.COD_PERSONA='" + CodPersona + "'";
                con.conectar();
                cn = con.getconection();
                st = cn.createStatement();
                rs = st.executeQuery(QQ);
                if (rs.next()) {
                    request.setAttribute("acta", rs.getString("COD_PERSONA"));
                    request.setAttribute("actaD", rs.getString("NRO_ACTA_CDEF"));
                    request.setAttribute("fecha", rs.getString("FECHA_REG_DEF"));
                    request.setAttribute("CDeU", rs.getString("id_ubigeo_dep"));
                    request.setAttribute("CPrU", rs.getString("id_ubigeo_prov"));
                    request.setAttribute("CDiU", rs.getString("id_ubigeo_dist"));
                    request.setAttribute("COtU", rs.getString("id_ubigeo_otro"));
                    request.setAttribute("NUD", rs.getString("nom_ubigeo_dep") + " " + rs.getString("nom_ubigeo_prov") + " " + rs.getString("nom_ubigeo_dist") + " " + rs.getString("nom_ubigeo_otro"));
                    request.setAttribute("lugar", rs.getString("NOM_CEN_LUG_OCU"));
                    request.setAttribute("fechaD", rs.getString("FECHA_DEF"));
                    request.setAttribute("hora", rs.getString("HORA_DEF"));
                    request.setAttribute("madre", Lista.getNombrePersona(rs.getString("DNI_MADRE")));
                    request.setAttribute("padre", Lista.getNombrePersona(rs.getString("DNI_PADRE")));
                    request.setAttribute("declarante", Lista.getNombrePersona(rs.getString("DNI_DECLARANTE")));
                    request.setAttribute("registrador", Lista.getNombrePersona(rs.getString("DNI_PERS_REG")));
                    request.setAttribute("estado", Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                    request.setAttribute("obs", rs.getString("OBSERVACIONES"));
                    request.setAttribute("persona", rs.getString("PRIM_APE_PERS") + " " + rs.getString("SEG_APE_PERS") + " " + rs.getString("PRE_NOM_PERS"));
                    request.setAttribute("ruta", rs.getString("RUTA_ACTA_DEF"));
                    request.getRequestDispatcher("/jsp/BAniDefuncion.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher("/jsp/BAniDefuncion.jsp").forward(request, response);
                }
            } catch (Exception z) {
                z.printStackTrace();
            }
        }


        if (BTN.equals("ActaCDEF")) {
            try {
                conexionsqlserver con = new conexionsqlserver();
                Connection cn = null;
                Statement st = null;
                ResultSet rs = null;
                String QQ = "select * from TB_DEFUNCION d,TB_PERSONA p,TB_UBIGEO ub,TB_CENT_LUG_OCURRE oc where (d.COD_PERSONA=p.COD_PERSONA and d.IDUBIGEO=ub.idubigeo and oc.COD_LUGAR_OCURR=d.COD_LUGAR_OCURR) and d.NRO_ACTA_CDEF='" + nroActaCDEF + "'";
                con.conectar();
                cn = con.getconection();
                st = cn.createStatement();
                rs = st.executeQuery(QQ);
                if (rs.next()) {
                    request.setAttribute("acta", rs.getString("COD_PERSONA"));
                    request.setAttribute("actaD", rs.getString("NRO_ACTA_CDEF"));
                    request.setAttribute("fecha", rs.getString("FECHA_REG_DEF"));
                    request.setAttribute("CDeU", rs.getString("id_ubigeo_dep"));
                    request.setAttribute("CPrU", rs.getString("id_ubigeo_prov"));
                    request.setAttribute("CDiU", rs.getString("id_ubigeo_dist"));
                    request.setAttribute("COtU", rs.getString("id_ubigeo_otro"));
                    request.setAttribute("NUD", rs.getString("nom_ubigeo_dep") + " " + rs.getString("nom_ubigeo_prov") + " " + rs.getString("nom_ubigeo_dist") + " " + rs.getString("nom_ubigeo_otro"));
                    request.setAttribute("lugar", rs.getString("NOM_CEN_LUG_OCU"));
                    request.setAttribute("fechaD", rs.getString("FECHA_DEF"));
                    request.setAttribute("hora", rs.getString("HORA_DEF"));
                    request.setAttribute("madre", Lista.getNombrePersona(rs.getString("DNI_MADRE")));
                    request.setAttribute("padre", Lista.getNombrePersona(rs.getString("DNI_PADRE")));
                    request.setAttribute("declarante", Lista.getNombrePersona(rs.getString("DNI_DECLARANTE")));
                    request.setAttribute("registrador", Lista.getNombrePersona(rs.getString("DNI_PERS_REG")));
                    request.setAttribute("estado", Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                    request.setAttribute("obs", rs.getString("OBSERVACIONES"));
                    request.setAttribute("persona", rs.getString("PRIM_APE_PERS") + " " + rs.getString("SEG_APE_PERS") + " " + rs.getString("PRE_NOM_PERS"));
                    request.setAttribute("ruta", rs.getString("RUTA_ACTA_DEF"));
                    request.getRequestDispatcher("/jsp/BAniDefuncion.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher("/jsp/BAniDefuncion.jsp").forward(request, response);
                }
            } catch (Exception z) {
                z.printStackTrace();
            }
        }
        if (BTN.equals("nombres")) {
            try {
                conexionsqlserver con = new conexionsqlserver();
                Connection cn = null;
                Statement st = null;
                ResultSet rs = null;
                String QQ = "select * from TB_DEFUNCION d,TB_PERSONA p,TB_UBIGEO ub,TB_CENT_LUG_OCURRE oc where (d.COD_PERSONA=p.COD_PERSONA and d.IDUBIGEO=ub.idubigeo and oc.COD_LUGAR_OCURR=d.COD_LUGAR_OCURR) and (p.PRE_NOM_PERS='" + nombre.toUpperCase() + "' and p.PRIM_APE_PERS='" + ape1.toUpperCase() + "' and p.SEG_APE_PERS='" + ape2.toUpperCase() + "')";
                con.conectar();
                cn = con.getconection();
                st = cn.createStatement();
                rs = st.executeQuery(QQ);
                if (rs.next()) {
                    request.setAttribute("acta", rs.getString("COD_PERSONA"));
                    request.setAttribute("actaD", rs.getString("NRO_ACTA_CDEF"));
                    request.setAttribute("fecha", rs.getString("FECHA_REG_DEF"));
                    request.setAttribute("CDeU", rs.getString("id_ubigeo_dep"));
                    request.setAttribute("CPrU", rs.getString("id_ubigeo_prov"));
                    request.setAttribute("CDiU", rs.getString("id_ubigeo_dist"));
                    request.setAttribute("COtU", rs.getString("id_ubigeo_otro"));
                    request.setAttribute("NUD", rs.getString("nom_ubigeo_dep") + " " + rs.getString("nom_ubigeo_prov") + " " + rs.getString("nom_ubigeo_dist") + " " + rs.getString("nom_ubigeo_otro"));
                    request.setAttribute("lugar", rs.getString("NOM_CEN_LUG_OCU"));
                    request.setAttribute("fechaD", rs.getString("FECHA_DEF"));
                    request.setAttribute("hora", rs.getString("HORA_DEF"));
                    request.setAttribute("madre", Lista.getNombrePersona(rs.getString("DNI_MADRE")));
                    request.setAttribute("padre", Lista.getNombrePersona(rs.getString("DNI_PADRE")));
                    request.setAttribute("declarante", Lista.getNombrePersona(rs.getString("DNI_DECLARANTE")));
                    request.setAttribute("registrador", Lista.getNombrePersona(rs.getString("DNI_PERS_REG")));
                    request.setAttribute("estado", Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                    request.setAttribute("obs", rs.getString("OBSERVACIONES"));
                    request.setAttribute("persona", rs.getString("PRIM_APE_PERS") + " " + rs.getString("SEG_APE_PERS") + " " + rs.getString("PRE_NOM_PERS"));
                    request.setAttribute("ruta", rs.getString("RUTA_ACTA_DEF"));
                    request.getRequestDispatcher("/jsp/BAniDefuncion.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher("/jsp/BAniDefuncion.jsp").forward(request, response);
                }
            } catch (Exception z) {
                z.printStackTrace();
            }
        } else {
            request.getRequestDispatcher("/jsp/BAniDefuncion.jsp").forward(request, response);
        }

    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
